package de.justphil.tcg.tcgserver.commons.util;

import java.util.Calendar;
import java.util.TimeZone;

public class DateTimeUtil {
	
	/* This static method is thread-safe. */
	public static long getUTCTimeInMillis() {
		/*
		 * Restricting access to the constructor of a class automatically doesn't makes it a Singleton.
		 * The Calendar class constructor is protected, and the class offers a getInstance() method to get
		 * an instance of the class. However, each call to getInstance() gets a new instance of the class.
		 * So that isn't a Singleton.
		 * Source: http://stackoverflow.com/questions/2475380/example-for-singleton-pattern/2475486#2475486
		 */
		Calendar c = Calendar.getInstance();

	    TimeZone z 		= c.getTimeZone();
	    int offset 		= z.getRawOffset();
	    int offsetHrs 	= offset / 1000 / 60 / 60;
	    int offsetMins 	= offset / 1000 / 60 % 60;

	    c.add(Calendar.HOUR_OF_DAY, (-offsetHrs));
	    c.add(Calendar.MINUTE, (-offsetMins));
	    
	    return c.getTimeInMillis();
	}

}
